package fr.cdj.thebox.client.app.ui;

import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.SubmitButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.inject.Inject;

import fr.cdj.thebox.client.content.ui.ContentDisplay;
import fr.cdj.thebox.client.menu.ui.MenuDisplay;
import fr.cdj.thebox.client.resources.TheboxBundle;

public class TheboxDisplayImpl extends FlowPanel implements TheboxDisplay {

	private TextBox loginInput;
	private TextBox passwdInput;
	private Button submitInput;

	private FormPanel loginForm;
	private SimplePanel menuContainer;
	private SimplePanel contentContainer;

	@Inject
	public TheboxDisplayImpl(TheboxBundle resource) {
		Label loginlabel = new Label("Login");
		loginlabel.setStyleName(resource.getStyle().loginLabel());
		loginInput = new TextBox();
		loginInput.setStyleName(resource.getStyle().loginInput());
		
		Label passwdLabel = new Label("Password");
		passwdLabel.setStyleName(resource.getStyle().passwdLabel());
		passwdInput = new PasswordTextBox();
		passwdInput.setStyleName(resource.getStyle().passwdInput());
		
		submitInput = new SubmitButton("Submit");
		submitInput.setStyleName(resource.getStyle().submitButton());
		
		FlowPanel loginContainer = new FlowPanel();
		loginContainer.setStyleName(resource.getStyle().loginPanel());
		loginContainer.add(loginlabel);
		loginContainer.add(loginInput);
		loginContainer.add(passwdLabel);
		loginContainer.add(passwdInput);
		loginContainer.add(submitInput);
		
		loginForm = new FormPanel();
		loginForm.setWidget(loginContainer);

		menuContainer = new SimplePanel();
		contentContainer = new SimplePanel();
	}

	@Override
	public void setMenuDisplay(MenuDisplay menuDisplay) {
		menuContainer.setWidget(menuDisplay.asWidget());
	}

	@Override
	public void setContentDisplay(ContentDisplay contentDisplay) {
		contentContainer.setWidget(contentDisplay.asWidget());
	}

	@Override
	public HasText getLoginHasText() {
		return loginInput;
	}

	@Override
	public HasText getPasswordHasText() {
		return passwdInput;
	}

	@Override
	public HandlerRegistration addLoginSubmitHandler(SubmitHandler handler) {
		return loginForm.addSubmitHandler(handler);
	}

	@Override
	public void showLogin(boolean show) {
		this.clear();
		if (show) {
			this.add(loginForm);
		} else {
			this.add(contentContainer);
			this.add(menuContainer);
		}
	}

	@Override
	public void invalidLogin() {
		passwdInput.setText("");
		passwdInput.getElement().setAttribute("invalid", "");
	}

	@Override
	public void clearLogin() {
		loginInput.setText("");
		passwdInput.setText("");
		passwdInput.getElement().removeAttribute("invalid");
	}

}
